$(function(){
	setHeight();
	initYYY();
	initUser();
	$JsApi_Share.init();
});
var timer = {},agrArr=[];
var authCode = "";// 保存短信校验码密文

var yyying = true;

/**
 * 动态设置位置
 */
function setHeight(){
	var w = window.innerWidth;
	var b = w * 99 / 560;
	$('.header .yyy img').css({
		'margin-top': (w * 440 / 660) + 'px',
	});
	$('.rule').css({
		'top': (w * 440 / 420) + 'px',
	});
}

/**
 * 初始化用户
 */
function initUser(){

}

var setTimeObj = {
	isStart: false,
	h: 0,
	m: 0,
	s: 0
};
function startSetTime(h, m, s) {
	setTimeObj.h = h;
	setTimeObj.m = m;
	setTimeObj.s = s;
	if(!setTimeObj.isStart) {
		setTimeObj.isStart = true;
		setTime();
	}
}

function setTime() {
	var text = "本时段结束时间还有";
	text += " " + setTimeObj.h + "小时" + setTimeObj.m + "分" + setTimeObj.s + "秒";
	$('.prizenum .prizetotal .text').html(text);
	setTimeout(function() {
		if(setTimeObj.s > 0) {
			setTimeObj.s--;
		} else {
			if( setTimeObj.m > 0) {
				setTimeObj.s = 59;
				setTimeObj.m--;
			} else {
				if(setTimeObj.h > 0) {
					setTimeObj.h--;
					setTimeObj.m = 59;
					setTimeObj.s = 59;
				}
			}
		}
		setTime(setTimeObj.h, setTimeObj.m, setTimeObj.s);
	}, 1000);
}

function initYYY() {
	if (window.DeviceMotionEvent) {
		var timestamp = 0;
		window.addEventListener('devicemotion', function(eventData) {
			if(yyying) {
		    	var a = eventData.accelerationIncludingGravity;
		    	var x = a.x;
		    	var y = a.y;
		    	var z = a.z;
		    	var xxyyzz = x * x + y * y + z * z;
		    	if(xxyyzz < 30 || xxyyzz > 180) {
			    	if(timestamp) {
			    		var temp = new Date().getTime();
			    		if(temp - timestamp < 500) {
			    			timestamp = 0;
			    			//摇一摇音频
			    			var media = $("#musicBox")[0];
							media.play(); 
							alert("活动已结束！");
			    			//lucky();
			    		} else {
			    			timestamp = temp;
			    		}
			    	} else {
			    		timestamp = new Date().getTime();
			    	}
		    	}
			}
	    }, false);
	} else {
	    alert('本设备不支持摇一摇功能');
	}
}

function lucky() { 
	yyying = false;
	$(".loadingmsg").show();
	$.ajax( {
		dataType : 'json',
		url :"http://www.gofull.com.cn/dwGamePlay.do",
		data : {},
		type : 'POST',
		beforeSend : function() {
		},
		success : function(data) {
			$(".loadingmsg").hide();
			var result = data.result;
			var html = "";
			var title = "";
			var content = "";
			var get_tel = "";
			var imgUrl = null;
			$(".modal .modal_content .btns").hide();
			if(data && data.success){
				if(result != 0){
					$("#awardId").val(result.awardId);
					title = "恭喜您中奖啦！";
					content = result.prizeName;
					imgUrl = '';
					//imgUrl = 'http://www.gofull.com.cn/game/dwGame/images/award.jpg';
					$(".modal .modal_content .btns.prized").show();
					$(".modal .modal_content .get_tel").show();
				}else{
					title = "不好意思！";
					content = "此次没有中奖";
					$(".modal .modal_content .btns.noprized").show();
					$(".modal .modal_content .get_tel").hide();
				}
			}else{
				title = "不好意思！"; 
				if(data.errors) {
					content = data.errors; 
				} else {
					content = "系统错误";
				}
				$(".modal .modal_content .btns.other").show();
				$(".modal .modal_content .get_tel").hide();
			}
			$(".modal .modal_content .title").html(title);
			$(".modal .modal_content .content").html(content);
			if(imgUrl) {
				$(".modal .modal_content .img").show();
				$(".modal .modal_content .img").attr('src', imgUrl);
			} else {
				$(".modal .modal_content .img").hide();
			}
			$(".modal").show();
		},
		error : function() {
		}
	});

}

function showModal() {
	$('.modal').show();
}

function hideModal() {
	yyying = true;
	$('.modal').hide();
	$(".modal .modal_content .btns.prized").hide();
	$(".modal .modal_content .get_tel").hide();
	$(".modal .modal_content .btns.getsuccess").hide();
	$(".modal .modal_content .btns.noprized").hide();
	$(".modal .modal_content .btns.other").hide();
}

function showLogin(){
	$('.login_modal').show();
}

function getLucky() {
	var phoneNum = $("#login_txt_cell").val();
	var awardId = $("#awardId").val();
	if(verifyPhoneNum(phoneNum)){
		var param = {};
		param["paramMap.phoneNum"] = phoneNum;
		param["paramMap.awardId"] = awardId;
		$.ajax( {
			dataType : 'json',
			url :"http://www.gofull.com.cn/dwGameGetlucky.do",
			data : param,
			type : 'POST',
			beforeSend : function() {
			},
			success : function(data) {
				if(data){
					var result = data.result;
					if(result == 'success'){
						title = "领取成功！";
						content = '恭喜您已成功领取奖品！';
						$(".modal .modal_content .title").html(title);
						$(".modal .modal_content .content").html(content);
						$(".modal .modal_content .img").hide();
						$(".modal .modal_content .btns.prized").hide();
						$(".modal .modal_content .get_tel").hide();
						$(".modal .modal_content .btns.getsuccess").show();
					}else if(result == 'entityAward'){
						title = "领取成功！";
						content = '话费或流量将在1个工作日内充值到您的手机账户！';
						$(".modal .modal_content .title").html(title);
						$(".modal .modal_content .content").html(content);
						$(".modal .modal_content .img").hide();
						$(".modal .modal_content .btns.prized").hide();
						$(".modal .modal_content .get_tel").hide();
						$(".modal .modal_content .btns.other").show();
					}else if(result = 'newUser'){
						var urlStr = '/getRedEnvelopesInit.do?mobile=' + phoneNum + '&senvKey=' + awardId;
						window.location.href = "http://www.gofull.com.cn" + urlStr;
					}
					
				}
			},
			error : function() {
			}
		});
	}
}

function giveupLucky() {
	hideModal();
}

function verifyPhoneNum(phoneNum) {
	if (phoneNum.length == 11 && !isNaN(phoneNum)) {
		if (!(/^1[3|4|5|7|8]\d{9}$/.test(phoneNum))) {
			alert("请输入有效的手机号码");
			return false;
		} 
	} else {
		alert("请输入有效的手机号码");
		return false;
	}
	return true;
}

function goVoucher() {
	window.location.href = "http://www.gofull.com.cn/wchatVoucher.do";
}

